package com.baijiayun.liveuibase.base;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;

public abstract class BaseTitleWindow extends BaseWindow {
    protected ConstraintLayout clTitleContainer;
    protected RelativeLayout clContentContainer;
    // region title views
    protected ImageView ivTitleIcon;
    protected TextView tvTitleName;
    protected ImageView ivTitleClose;
    // endregion

    public BaseTitleWindow(Context context) {
        super(context);
        initView();
        onCreateTopView(context);
        onCreateContentView(context);

        initTitleContainer();
    }

    protected void initTitleContainer() {
        ivTitleIcon = view.findViewById(R.id.base_window_title_icon);
        ivTitleClose = view.findViewById(R.id.base_window_title_close);
        tvTitleName = view.findViewById(R.id.base_window_title_name);

        ivTitleClose.setOnClickListener(v -> {
            onCloseWindow();
        });
    }

    private void initView() {
        clTitleContainer = view.findViewById(R.id.uibase_window_title_container);
        clContentContainer = view.findViewById(R.id.uibase_window_content_container);
    }

    @Override
    protected View onCreateView(Context context) {
        setNeedShowShadow(true);
        final View view = View.inflate(context, R.layout.uibase_window_common_layout, null);
        view.setBackground(ThemeDataUtil.getCommonWindowBg(context));
        return view;
    }

    protected void onCreateTopView(Context context){
        LayoutInflater.from(context).inflate(R.layout.uibase_window_common_title_layout, clTitleContainer);
    }

    protected abstract void onCreateContentView(Context context);

    protected abstract void onCloseWindow();

    public void setRouterListener(RouterListener routerListener) {
        this.routerListener = routerListener;
    }

    public void setTitle(String name) {
        if (tvTitleName != null) {
            tvTitleName.setText(name);
        }
    }

    public void setIcon(Drawable iconDrawable) {
        if (ivTitleIcon != null) {
            ivTitleIcon.setImageDrawable(iconDrawable);
        }
    }
}
